/*
 * Copyright (c) 2010 Mathias Garbe <code@mathias-garbe.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "AutoUpdateWindow.hpp"

AutoUpdateWindow::AutoUpdateWindow(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);
    QRect screen = QApplication::desktop()->screenGeometry();
    this->move(screen.width()/2 - this->width()/2, screen.height()/2 - this->height()/2);
}

AutoUpdateWindow::~AutoUpdateWindow()
{

}

void AutoUpdateWindow::show()
{
    QWidget::show();
    this->close = false;
}

void AutoUpdateWindow::setTitle(std::string title)
{
    this->setWindowTitle(title.c_str());
}

void AutoUpdateWindow::setInfoLabel(std::string text)
{
    this->ui.labelInfo->setText(text.c_str());
}

void AutoUpdateWindow::setPercentage(int percent)
{
    this->ui.progressBar->setValue(percent);
}

void AutoUpdateWindow::closeEvent(QCloseEvent *event)
{
    QMessageBox msgBox;
    msgBox.setWindowTitle("CnCNet Update");
    msgBox.setText("The Auto-Update is currently downloading the latest update.");
    msgBox.setInformativeText("Do you want to cancel the download?");
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    msgBox.setIcon(QMessageBox::Warning);
    int ret = msgBox.exec();

    if(ret == QMessageBox::No)
    {
        event->ignore();
    }
    else
    {
        this->close = true;
        event->accept();
    }
}
